//
//  RecordView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/9/17.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

@objc protocol RecordViewDelegate {
    @objc optional func selectRecordType(title:String,index:Int) //选择记录类型
}

class RecordView: UIView {

    weak var recordDelegate : RecordViewDelegate?
    
    fileprivate let cellId = "recordCell"
    
    fileprivate let dataArray = [shenkaRecord,loanRecord,financialRecord,insuranceRecord]
    
    fileprivate let colorArray = [UIColor.recordYellow(),UIColor.recordBlue(),UIColor.recordGreen(),UIColor.recordRed()]
    
    fileprivate var collectionView : UICollectionView!
    
    init() {
        super.init(frame: .zero)
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 3
        layout.minimumInteritemSpacing = 3
        layout.itemSize = CGSize.init(width: (ScreenW-9)/2, height: (ScreenH-StatusBarAddNavBarHeight-9)/2)
        layout.sectionInset = UIEdgeInsets.init(top: 3, left: 3, bottom: 3, right: 3)
        collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.white
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(RecordCell.self, forCellWithReuseIdentifier: cellId)
        self.addSubview(collectionView)
        
        collectionView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.zero)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension RecordView : UICollectionViewDelegate,UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as? RecordCell
        cell?.titleLabel.text = dataArray[indexPath.row]
        cell?.contentView.backgroundColor = colorArray[indexPath.row]
        return cell!
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)
        
        self.recordDelegate?.selectRecordType!(title: dataArray[indexPath.row], index: indexPath.row)
        
    }
    
}
